<?php

class Application_Model_DebtMapper extends Application_Model_Mapper
{
    public function __construct()
    {
        parent::__construct(new Application_Model_DbTable_Debts(), new Application_Model_Debt());
    }

    public function getDebtsByType($type = Application_Model_Debt::TYPE_I_HAVE_TO)
    {
        if ($type) {
            return $this->getCollection(array('type_debt' => $type));
        }

        return array();
    }

    public function getSumDebts()
    {
        $rows = $this->table->fetchAll($this->table->select()->from($this->table, array('SUM(`debt_summ`) as debts_sum', 'type_debt'))->group('type_debt'))->toArray();

        $result = array(
            Application_Model_Debt::TYPE_MY_HAVE_TO => 0,
            Application_Model_Debt::TYPE_I_HAVE_TO => 0
        );

        if ($rows) {
            foreach ($rows as $row) {
                switch ($row['type_debt']) {
                    case Application_Model_Debt::TYPE_MY_HAVE_TO:
                        $result[Application_Model_Debt::TYPE_MY_HAVE_TO] = $row['debts_sum'];
                        break;
                    case Application_Model_Debt::TYPE_I_HAVE_TO:
                        $result[Application_Model_Debt::TYPE_I_HAVE_TO] = $row['debts_sum'];
                        break;
                }
            }
        }

        return $result;
    }
}
